﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CNRegex.Web.Base;
using CNRegex.Biz.BizAccess;
using CNRegex.Web.Models;
using CNRegex.Entity;
using CNRegex.Entity.Others;
using CNRegex.Enum;
using CNRegex.Biz;

namespace CNRegex.Web.Controllers
{
	public class RegexHomeController : BaseController
	{
		private BizRegexExpressionComment bizRegexExpressionComment;
		public RegexHomeController()
		{

		}

		public ActionResult Index(string id,int? pageIndex)
		{
			return View(new ModelRegexHome() { RegexCategoryName = id, PageIndex = pageIndex });
		}

		public ActionResult Search(Guid? c, int? p, string k, string ok, int? d, SearchExpressionEntity searchExpressionEntity)
		{
			searchExpressionEntity.UserExpressionCatalogID = c;
			searchExpressionEntity.Keywords = k;
			if (string.IsNullOrEmpty(ok) || d == null)
			{
				searchExpressionEntity.OrderRecommend = null;
				searchExpressionEntity.OrderClick = null;
			}
			else
			{
				if (ok.Trim() == "click" && d == 0)
					searchExpressionEntity.OrderClick = EnumOrder.ASC;
				if (ok.Trim() == "click" && d == 1)
					searchExpressionEntity.OrderClick = EnumOrder.DESC;
				if (ok.Trim() == "recommend" && d == 0)
					searchExpressionEntity.OrderRecommend = EnumOrder.ASC;
				if (ok.Trim() == "recommend" && d == 1)
					searchExpressionEntity.OrderRecommend = EnumOrder.DESC;
				if (ok.Trim() == "date" && d == 0)
					searchExpressionEntity.OrderCreateDate = EnumOrder.ASC;
				if (ok.Trim() == "date" && d == 1)
					searchExpressionEntity.OrderCreateDate = EnumOrder.DESC;
			}

			ModelRegexSearch model = new ModelRegexSearch() { SearchExpression = searchExpressionEntity, PageIndex = p };
			return View(model);
		}

		public ActionResult Detail(string id, int? pageIndex)
		{
			return View(new ModelRegexDetail(id) { PageIndex = pageIndex });
		}

		[HttpPost]
		[ValidateInput(false)]
		public ActionResult Comment(RegexExpressionComment model,FormCollection fc)
		{
			model.CommentDate = DateTime.Now;
			model.UserID = FrontContext.CurrentRegexUser.ID;
			bizRegexExpressionComment.Save(model);
			return RedirectToAction("Detail", new { id = model.ExpressionID });
		}

	}
}
